<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ page language="java" import="com.qipay.admin.tags.PageModel" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.admin.utils.SiteHelper" %>
<%@ page import="com.qipay.baas.model.Sp" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="com.qipay.admin.sp.vo.SpUserDTO" %>
<%@ page import="com.qipay.core.utils.CommUtil" %>
<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/list.css">
    <script type="text/javascript">
        //document.getElementsByTagName("html")[0].style.width=screen.availWidth/100+"px";
        document.getElementsByTagName("html")[0].style.fontSize = screen.availWidth / 200 + "px";
    </script>
    <style type="text/css">
        .border {
            border: 1px solid red;
        }
    </style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-md-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">通道（SP）服务列表</h3>
                            <a href="${pageContext.request.contextPath}/sp/add">
                                <button type="button" class="button back-green" style="font-size: 1rem">添加SP</button>
                            </a>
                        </div>
                        <div>
                            <div class="new-group">
                                <label> SP名称：</label>
                                <input type="text" id="txtName"
                                       value="<%= request.getParameter("name")!=null?request.getParameter("name"):""%>"/>
                            </div>
                            <div class="new-group">
                                <label> 服务：</label>
                                <input type="text" id="txtPerson"
                                       value="<%= request.getParameter("service")!=null?request.getParameter("service"):""%>"/>
                            </div>
                            <div class="new-group">
                                <label> 渠道：</label>
                                <input type="text" id="txtContract"
                                       value="<%= request.getParameter("bank")!=null?request.getParameter("bank"):""%>"/>
                            </div>
                            <%
                                List<SpUserDTO> spUserDTOS = (List<SpUserDTO>) request.getAttribute("spUsers");
                            %>
                            <div class="new-group">
                                <label> 供号商：</label>
                                <select id="spUserName" name="spUserName">
                                    <option value="">全部</option>
                                    <% for (SpUserDTO s : spUserDTOS) {%>
                                    <option value="<%=s.getName()%>"><%= s.getspUserName()%>
                                            <%}%>
                                </select>
                            </div>
                            <div class="new-group">
                                <label> 状态：</label>
                                <select id="ddltype">
                                    <option value="-1">全部</option>
                                    <option value="0">正常</option>
                                    <option value="1">禁用</option>
                                </select>
                            </div>
                            <button type="button" class="button back-green" id="btnSearch" style="font-size: 1rem">查询
                            </button>
                            <div style="clear: both"></div>
                        </div>
                        <!-- /.box-header -->
                        <div class="tableContent">
                            <table width="100%" cellspacing="0" cellpadding="0" border="1">
                                <thead>
                                <tr>
                                    <th width="3%">ID</th>
                                    <th width="5%">SP名称</th>
                                    <%--<th width="8%">商户ID</th>--%>
                                    <th width="5%">服务</th>
                                    <th width="5%">渠道</th>
                                    <th width="5%">供号商</th>
                                    <th width="5%">银行卡号</th>
                                    <th width="5%">加入时间</th>
                                    <th width="3%">状态</th>
                                    <th width="10%">通道状态</th>
                                    <th width="10%">收款支付宝号</th>

                                    <th width="30%">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <% List<Sp> articles = (List<Sp>) request.getAttribute("lst");
                                    PageModel pager = (PageModel) request.getAttribute("page");
                                    String query = pageContext.getRequest().getServletContext().getContextPath() + "/sp" + pager.getUrl();
                                    query = StringUtils.replace(query, "s=null", "s=");
                                    if (articles != null && articles.size() > 0) {
                                        Iterator<Sp> iter = articles.iterator();
                                        Sp user = null;
                                        while (iter.hasNext()) {
                                            user = iter.next();

                                %>
                                <tr id="user_<%=user.getId()%>">
                                    <td><%=user.getId()%>
                                    </td>
                                    <td><%=user.getName()%>
                                    </td>
                                    <%--<td><%=user.getMchId()%>--%>
                                    </td>
                                    <td>
                                        <%= user.getService()%>
                                    </td>
                                    <td>
                                        <a href="${pageContext.request.contextPath}/sp?bank=<%= user.getBank()%>"
                                           style="color: #00a65a">
                                            <%= user.getBank()%>
                                        </a>
                                    </td>
                                    <td>
                                        <%= user.getSpUserName()%>
                                    </td>
                                    <td>
                                        <%= user.getCardNo()%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getDefaultTimeFormat(user.getCreateDate())%>
                                    </td>
                                    <td id="trgoods<%= user.getId()%>"
                                        class="<%= user.getState()!=null&&user.getState()==0?"success":""%>"><%= SiteHelper.getChannelStateString(user.getState())%>
                                    </td>
                                    <td class="failReason">
                                        <%if (user.getState()==0) {%>
                                        <span style="color: #00e765">正常</span>
                                        <%} else {%>
                                        <span style="color: red"><%= user.getFailReason()==null?"手动禁用":user.getFailReason()%></span>
                                        <%}%>
                                    </td>
                                    <td>
                                        <a href="#" onclick="preEditZfbNo(<%=user.getId()%>,'<%=user.getZfbNo()%>')"
                                           style="color: #00a65a" data-toggle="modal" data-target="#editZfbNoModal">
                                            <%= CommUtil.isNotEmpty(user.getZfbNo()) ? user.getZfbNo() : "无支付宝号"%>
                                        </a>
                                    </td>
                                    <td>
                                        <a href="${pageContext.request.contextPath}/sp/<%= user.getId()%>/channel">
                                            <button type="button" class="button back-green" style="font-size: 1rem">渠道
                                            </button>
                                        </a>
                                        <a href="${pageContext.request.contextPath}/sp/<%= user.getId()%>/config">
                                            <button type="button" class="button back-green" style="font-size: 1rem">
                                                参数配置
                                            </button>
                                        </a>
                                        <a href="${pageContext.request.contextPath}/sp/<%= user.getId()%>/api">
                                            <button type="button" class="button back-green" style="font-size: 1rem">
                                                api配置
                                            </button>
                                        </a>
                                        <a href="${pageContext.request.contextPath}/sp/<%= user.getId()%>">
                                            <button type="button" class="button back-green" style="font-size: 1rem">修改
                                            </button>
                                        </a>
                                        <a name="lnkDelete"
                                           s="<%= user.getState()!=null&&user.getState()==0?"200":"100"%>"
                                           pk="<%= user.getId()%>" href="javascript:;">
                                            <button type="button" class="button back-red"
                                                    style="font-size: 1rem"><%= user.getState() != null && user.getState() == 0 ? "禁用" : "启用"%>
                                            </button>
                                        </a>

                                        <%-- //删除标记--%>
                                        <a name="deleteFlag"
                                           href="javascript:;" pk="<%= user.getId()%>">
                                            <button type="button" class="button back-red" style="font-size: 1rem">删除
                                            </button>
                                        </a>

                                    </td>
                                </tr>
                                <%
                                    }
                                } else {
                                %>
                                <tr>
                                    <td colspan="5">no data.</td>
                                </tr>
                                <% }%>
                                </tbody>
                            </table>
                            <div class="col-sm-10">
                                <div class="dataTables_paginate paging_simple_numbers" id="example1_paginate">
                                    <page:page totalSum="<%= pager.getTotal()%>"
                                               url="<%= query%>"
                                               pageNo="<%= pager.getPage()%>"
                                               pageSize="<%= pager.getPageSize()%>"></page:page>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
    <!-- /.content-wrapper -->
    <jsp:include page="foot.jsp"></jsp:include>

    <!-- Modal 修改支付宝号-->
    <div class="modal fade" id="editZfbNoModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title control-label" id="editZfbNoModallabel">修改支付宝号</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group form-group-md">
                            <label class="col-sm-2 control-label" for="zfbNo">支付宝账号:</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" id="zfbNo" placeholder="请输入收款支付宝号">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">放弃</button>
                    <button type="button" class="btn btn-primary" onclick="sureZfbNo()">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.slimscroll.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/fastclick.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script>
    var zfbNoUser = null;

    /**
     * 弹出修改支付宝账号前
     */
    function preEditZfbNo(id, zfbNo) {
        zfbNoUser = {id: id, zfbNo: zfbNo};
        $('#zfbNo').val(zfbNo=='null'?'':zfbNo);
    }

    /**
     * 修改支付宝账号发起请求,调后端修改
     */
    function sureZfbNo() {
        var zfbNo = $('#zfbNo').val();
        $.ajax({
            url: contextPath + "/sp/saveZfbNo",
            data: {id: zfbNoUser.id, zfbNo: zfbNo},
            type: "POST",
            async: true,//true异步 false同步
            context: this,
            timeout: 10000,
            success: function (data) {
                if (data && data.code == 200) {
                    <%--location.href = '${pageContext.request.contextPath}/sp';--%>
                    location.reload();
                }
            },
            error: function (d, s, b) {
                console.log("失败:", b);
            }
        });
    }

    $(function () {
        var channelState = parseInt("<%= StringUtils.isNotEmpty(request.getParameter("s"))?request.getParameter("s"):-1%>", 10);
        $("#ddltype").val(channelState);
        $("#btnSearch").on("click", function () {
            var name = $.trim($("#txtName").val());
            var service = $.trim($("#txtPerson").val());
            var bank = $.trim($("#txtContract").val());
            var spUserName = $.trim($("#spUserName").val());
            var s = $("#ddltype").val();
            if (s == null || s == "null") {
                s = -1;
            }
            window.location.href = "${pageContext.request.contextPath}/sp?spUserName=" + spUserName + "&name=" + encodeURIComponent(name) + "&service=" + encodeURIComponent(service) + "&bank=" + encodeURIComponent(bank) + "&s=" + (s == -1 ? "" : s);
        });
        $("[name='lnkRemove']").click(function () {
            var pk = $(this).attr("pk");
            if (pk > 0) {
                if (confirm("确定删除吗？")) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=delspbyid&id=" + pk,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                $("#user_" + pk).hide(100);
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }

        });


        //确定标记删除
        $("[name='deleteFlag']").click(function () {
            var pk = $(this).attr("pk");
            if (pk > 0) {
                if (confirm("确定删除吗？")) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=setspdeleteflag&id=" + pk,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                $("#user_" + pk).hide(100);
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }

        });


        $("[name='lnkDelete']").click(function () {
            var that = $(this);
            var pk = $(this).attr("pk");
            var state = $(this).attr("s");
            var isSuccess = state == "200";
            if (confirm(isSuccess ? "确定禁用吗？" : "确定启用吗？")) {
                state = isSuccess ? 1 : 200;
                if (pk > 0) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=setsplstate&id=" + pk + "&id2=" + state,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                if (isSuccess) {
                                    $("#trgoods" + pk).removeClass("success");
                                    $("#trgoods" + pk).html("禁用");
                                    $(this).html("<button type=\"button\" class=\"button back-red\">启用</button>");
                                    $(this).attr("s", "100");
                                    $(this).parent().siblings(".failReason").html("<span style=\"color: red\">手动禁用</span>");
                                }else {
                                    $("#trgoods" + pk).addClass("success");
                                    $("#trgoods" + pk).html("启用");
                                    $(this).html("<button type=\"button\" class=\"button back-red\">禁用</button>");
                                    $(this).attr("s", "200");
                                    $(this).parent().siblings(".failReason").html("<span style=\"color: #00e765\">正常</span>");
                                }
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }
        });
    });
</script>
</body>
</html>

